package com.c9.config;

import com.c9.dao.PermissionRepository;
import com.c9.dao.UserRepository;
import com.c9.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserRepository userRepository;
    PermissionRepository permissionRepository;
    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        UserDetailsService userDetailsService = new UserDetailsService() {
            @Override
            public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
                User user=userRepository.findByUsername(username);
                if(user==null) throw new UsernameNotFoundException("用户不存在");
                return userRepository.findByUsername(username);
            }
        };
        auth.userDetailsService(userDetailsService);
//        auth.inMemoryAuthentication()
//                .withUser("negen")
//                .password("$10$FhA.2S4yo488TFAcUtcKue6erw.9rORRC3VC/gurbFbW/CVUu7Cj2")
//                .roles("admin")
//                .and()
//                .withUser("$10$95.jLb1.obTFG3fhtCOI7O4S2sQhcU/RD90CudgFttWeiEeQ1Pqne")
//                .password("111111")
//                .roles("user")
//                .and()
//                .withUser("黄hl")
//                .password("$10$7vfxifjCIlvsydD3ExhGOeFg/cfLCx4HMKdu6bb0yvzMs.CUd3mwi")
//                .roles("dba");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**").hasRole("admin")
                .antMatchers("/user/**").hasRole("user")
                .antMatchers("/dba/**").hasRole("dba")
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O object) {
                        object.setSecurityMetadataSource(new CustomFilterInvocationSecurityMetadataSource(permissionRepository));
                        object.setAccessDecisionManager(new MyAccessDecisionManager());
                        return object;
                    }
                })
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginProcessingUrl("/login").permitAll()
                .and()
                .csrf().disable();
//                .anyRequest().authenticated()
//                .and()
//                .formLogin()
//                .loginProcessingUrl("/login").permitAll()
//                .and()
//                .csrf().disable();

    }
}
